Operator Overloading
Operator overloading is related to method overloading. Normally we cannot use operators on objects. By using operator overloading we can use the objects on operators. All the operators we can overload .There are several operators that you cannot overload. Perhaps most significantly, you cannot overload any assignment operator, including the compound assignments, such as +=.

 

It is two types 1) unary operator overloading 2) binary operator overloading.

 
General form for overloading a unary operator.
 
public static ret-type operator op(param-type operand)
{
    operations
}
 
 General form for overloading a binary operator.
 
public static ret-type operator op(param-type1 operand1, param-type1 operand2)
{
   operations
}

Program on unary operator overloading

using System;
class abc
{
public  int a,b,c;
public abc(int x,int y,int z)
{
a=x;
b=y;
c=z;
}
public  static abc  operator -(abc x)
{
abc k=new abc(0,0,0);
k.a =-x.a ;
k.b=-x.b;
k.c =-x.c ;
return k;

    }
public void put()
{
Console.WriteLine("{0}  {1}  {2}", a, b, c);
}

}

class sample
{
public static void Main()
{
abc x = new abc(5, -3, 7);
x.put();
abc k = -x;
k.put();
}
}

Program on binary operator overloading

using System;
class abc
{
public  int a,b;
public void get(int x,int y)
{
a=x;
b=y;

}
public  static abc  operator +(abc x,abc y)
{
abc k = new abc();
k.a = x.a + y.a;
k.b = x.b + y.b;
return k;
}
public void put()
{
Console.WriteLine("Sum of 2nos{0}",(a+b) );
}

}

class sample
{
public static void Main()
{
abc x = new abc();
abc y=new abc();

        x.get(6, 8);
x.put();
y.get(5,2);
y.put();
abc z;
z=x+y;
z.put();

    }
}

 

Program on unary ++

using System;
class abc
{
public  int a,b;
public void get(int x,int y)
{
a=x;
b=y;

}
public  static abc  operator ++(abc x)
{
x.a++;
x.b++;
return x ;

    }
public void put()
{
Console.WriteLine("Value of a{0}", a);
Console.WriteLine("Value of b{0}", b);
}

}

class sample
{
public static void Main()
{
abc x = new abc();
abc y;
x.get(6, 8);
x.put();
y = ++x;
y.put();
}
}

program to overload > and < operators

An important restriction applies to overloading the relational operators: You must overload them in pairs. For example, if you overload <, you must also overload >, and vice versa. The operator pairs are


= =

!=

<

>

<=

>=

true

false

 

using System;
class abc
{
public  int a,b;
public void get(int x,int y)
{
a=x;
b=y;

}
public  static bool   operator >(abc x,abc y)
{

        if (x.a > y.a && x.b > y.b)
return true;
else
return false;

 

    }
public static bool operator <(abc x, abc y)
{

        if (x.a < y.a && x.b < y.b)
return true;
else
return false;

 

    }

    public void put()
{
Console.WriteLine("Value of a{0}", a);
Console.WriteLine("Value of b{0}", b);
}

}

class sample
{
public static void Main()
{
abc x = new abc();
abc y = new abc();
x.get(6, 8);
y.get(5, 9);
x.put();
y.put();
if (x > y)
{
Console.Write("X object values are greatar than Y object");
}
else
{
Console.WriteLine("y object values are greater than x");
}


}
}

Program on True and False Operator overloading
using System;
class abc
{
public  int a;
public void get(int x)
{
a=x;

}

public static bool operator true(abc x)
{

        if (x.a ==0 )
return false;
else
return true ;

 

    }
public static bool operator false(abc x)
{

        if (x.a == 0)
return true ;
else
return false  ;

 

    }
public void put()
{
Console.WriteLine("Value of a{0}", a);

}

}

class sample
{
public static void Main()
{
abc x = new abc();
abc y = new abc();
x.get(6);
y.get(0);
x.put();
y.put();
if (x )
{
Console.Write("X object values are greatar than Zero");
}
else
{
Console.WriteLine("X object values are Zero");
}
if (y)
{
Console.WriteLine("Y object values are Greater than Zero");
}
else
{
Console.WriteLine("Y object valuer are equals to Zero");
}


}
}